﻿using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FormView.UI.PromptView
{
	public class PromptSpeedView : IPromptViewControl
	{
		public static readonly string TagName = "speed";

		public object GetValue(FrameworkElement element)
		{
			Grid grid = element as Grid;
			Slider slider = grid.Children[1] as Slider;
			return slider.Value;
		}

		public FrameworkElement GetElement(PromptFormWindow win, string tipText, object value)
		{
			double min, max, defaultVal;
			ParseSpeed(ref tipText, out min, out max);
			if (value == null)
			{
				defaultVal = 0;
			}
			else
			{
				if (!double.TryParse(value.ToString(), out defaultVal))
				{
					throw new ArgumentException("Prompt speed组件的默认值必须为数值类型");
				}
			}

			if (defaultVal < min || defaultVal > max)
			{
				throw new ArgumentException("Prompt speed组件的默认值必须处于最小值到最大值之间");
			}

			Grid grid = new Grid();
			grid.Background = PromptFormWindow.DefaultBgColor;
			grid.RowDefinitions.Add(new RowDefinition());
			grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = GridLength.Auto});
			grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star) });
			grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = GridLength.Auto, MinWidth = 50});

			TextBlock textBlock = new TextBlock()
			{
				Padding = new Thickness(2),
				Foreground = PromptFormWindow.DefaultForeColor,
				Text = tipText,
				TextAlignment = TextAlignment.Center
			};
			Grid.SetColumn(textBlock, 0);
			grid.Children.Add(textBlock);

			Slider slider = new Slider()
			{
				Value = defaultVal,
				Minimum = min,
				Maximum = max,
				Height = 10,
				Style = win.Resources["SliderStyle1"] as Style,
				Foreground = PromptFormWindow.DefaultForeColor,
				Background = new SolidColorBrush(Colors.Transparent),
				Padding = new Thickness(0, 2, 0, 2),
				BorderThickness = new Thickness(0, 1, 0, 0),
				BorderBrush = new SolidColorBrush(PromptFormWindow.ParseColorStr("#77FFFFFF"))
			};
			Grid.SetColumn(slider, 1);
			grid.Children.Add(slider);

			TextBlock displayTbx = new TextBlock()
			{
				Padding = new Thickness(5,2,5,2),
				Foreground = PromptFormWindow.DefaultForeColor,
				Background = new SolidColorBrush(PromptFormWindow.ParseColorStr("#33000000")),
				Margin = new Thickness(0),
				TextAlignment = TextAlignment.Center,
				Text = defaultVal.ToString("F")
			};
			Grid.SetColumn(displayTbx, 2);
			grid.Children.Add(displayTbx);

			slider.ValueChanged += (sender, args) =>
			{
				displayTbx.Text = args.NewValue.ToString("F");
			};

			return grid;
		}

		private void ParseSpeed(ref string value, out double min, out double max)
		{
			var regex = new Regex(@"(\[\s*(\-?\d+\.?\d*)\s*;\s*(\-?\d+\.?\d*)\s*])");
			var match = regex.Match(value);
			if (!match.Success)
			{
				throw new ArgumentException("speed组件的附加项应为数组范围，例：[10; 20]");
			}

			min = double.Parse(match.Groups[2].Value);
			max = double.Parse(match.Groups[3].Value);
			if (min > max)
			{
				throw new ArgumentException("解析Prompt Speed组件失败，speed的最小值不可大于最大值");
			}

			value = value.Split('[')[0].Trim();
		}

		public void OnRenderView(PromptFormWindow promptWindow, FrameworkElement element)
		{
			// ignore
		}
	}
}
